<?php
namespace jy_teamshop;
defined('BY_JYA') or exit('error');
/*
	任务
*/
class task extends \plugin{
	/*
		团UP
	*/
	public function teamup(){
		$pluginset=$this->plugin_setting('basic');
		$params=array('plugin_name'=>'jy_teamshop','status'=>0);
		$team = pdo_getall('jy_weishop_groups_team',$params,array('id','uniacid','goodsid','createtime'));
		if($team){
			foreach($team as $a){
				$_SESSION['uniacid']=$a['uniacid'];
				$goods=pdo_get('jy_weishop_goods',array('id'=>$a['goodsid']));
				$goods=$this->model('model')->up_price($goods,$pluginset,true);
				$goods['grouptime']=$goods['grouptime']?:10;
				$endtime=$goods['grouptime']*60+$a['createtime'];
				if(time()>$endtime){
					$this->model('model')->order_cancel($a['id']);
				}				
			}
		}
	}
	/*
		自动开团
	*/
	public function openteam(){
		$params=array('status'=>1);
		$goods = pdo_getall('jy_teamshop_goods',$params);
		if($goods){
			foreach($goods as $item){
				$_SESSION['uniacid']=$item['uniacid'];
				$this->model('order')->openteam($item);	
			}
		}
		return false;
	}
	/*
		自动预约
	*/
	public function add_yuyue(){
		$basic=$this->plugin_setting('basic');
		$params=array('status'=>1);
		$goods = pdo_getall('jy_teamshop_goods',$params);
		if($goods){
			foreach($goods as $item){
				$_SESSION['uniacid']=$item['uniacid'];
				$times=$this->model('model')->get_times($item['timeids'],true);
				if($times){
					$startime = strtotime(date('Y-m-d ' . $times));
					$jiqi_yuyuetime=$basic['jiqi_yuyuetime']?:30;
					if(TIMESTAMP+$jiqi_yuyuetime*60>$startime){
						$params=array('pingid'=>$item['id']);
						$canyu = pdo_getall('jy_teamshop_canyu',$params);
						if($canyu){
							foreach($canyu as $cv){
								$this->model('model')->set_yuyue($item['id'],true,$cv['mid'],$cv['addressid']);
							}
						}
					}
				}
			}
		}
	}
	/*
		自动参与
	*/
	public function add_ping(){
		$pluginset=$this->plugin_setting('basic');
		$params=array('status'=>1);
		$goods = pdo_getall('jy_teamshop_goods',$params);
		if($goods){
			foreach($goods as $item){
				$_SESSION['uniacid']=$item['uniacid'];
				$timeid=$this->model('model')->get_timeid($item['timeids']);
				if(!$timeid || $timeid=='fail'){
					continue;
				}
				$params=array('pingid'=>$item['id']);
				$params['times']=$timeid;
				$params['dayid']=date('Y-m-d',TIMESTAMP);
				$yuyues = pdo_getall('jy_teamshop_yuyue',$params);
				if($yuyues){
					foreach($yuyues as &$cv){
						//未满次的靠后
						$canzhong=$this->model('model')->check_canzhong($item['id'],$cv['mid']);
						$cv['canzhong']=$canzhong?1:0;
					}
					paixu($yuyues,'canzhong','asc');
					foreach($yuyues as $yv){
						if(!$yv['mid']){
							continue;
						}
						$this->model('order')->create($item,$yv);				
					}
				}
			}
		}
	}
	/*
		测试升级
	*/
	public function test(){		
		$pinggoods = pdo_get('jy_teamshop_goods',array('uniacid'=>$_SESSION['uniacid'],'id'=>1));
		$params=array(
			'pingid'=>$pinggoods['id'],
			'msg'=>'参与拼团成功！',
			'status'=>1,
			'orderid'=>1660,
			'dayid'=>date('Y-m-d',TIMESTAMP),
			'mid'=>1,
		);
		$this->model('renwu')->canyu($params,$pinggoods);
	}
}